package scuba.DiveBuddy.Class;

import scuba.DiveBuddy.data.AirMixSource;

/**
 * @author Alexander Anderson
 * @since 27/01/14
 * @version 1
 */
public class AirMix {
    //Fields
    private int ID;
    private String Name;
    private float O2;
    private float Nitrogen;
    private float Helium;
    private boolean Locked;

    //ctor
    public AirMix(String name, float o2, float n, float h) {
        //Create new
        this.Name = name;
        this.O2 = o2;
        this.Nitrogen = n;
        this.Helium = h;
        this.Locked = false;
    }
    public AirMix(int id, String name, float o2, float n, float h, boolean locked) {
        //Created from Database
        this.Name = name;
        this.ID = id;
        this.O2 = o2;
        this.Nitrogen = n;
        this.Helium = h;
        this.Locked = locked;
    }

    //Methods
    public float getID() { return this.ID; }

    public String getName() { return this.Name; }
    public void setName(String name) {
        this.Name = name;
    }

    public float getO2() { return this.O2; }
    public void setO2(float o2) {
        this.O2 = o2;
    }

    public float getN() { return this.Nitrogen; }
    public void setN(float n) {
        this.Nitrogen = n;
    }

    public float getH() { return this.Helium; }
    public void setH(float h) {
        this.Helium = h;
    }

    public boolean isLocked() { return this.Locked; }
    public void Lock() {
        AirMixSource.LockMix(this);
    }

    public static AirMix[] GetMixes() {
        return AirMixSource.GetMixes();
    }
    public static AirMix GetByID(int id) {

        for(AirMix mix : GetMixes())
            if(mix.getID() == id)
                return mix;

        return null; //nothing found :/
    }

    public boolean Commit() {
        if(this.ID == -1)  { //new, create
            this.ID = AirMixSource.CreateMix(this);
            return true;
        }
        else {
            return AirMixSource.UpdateMix(this);
        }
    }


    public String getLongDesc() {
        return String.format("Mix '%s' O2:%f N:%f H:%f", getName(), getO2(), getN(), getH());
    }

    public static Boolean CreateMix(String name, float o, float n, float h) {

        AirMix m = new AirMix(name, o, n, h);
        return m.Commit();
    }

    @Override
    public String toString() {
        return getName();
    }
}
